package com.nuoniu.sibanyun.controller.crm;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.crm.CrmQuotedPrice;
import com.nuoniu.sibanyun.entity.crm.CrmQuotedPriceDetails;
import com.nuoniu.sibanyun.service.crm.ICrmQuotedPriceService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 报价单 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-06-22
 */
@RestController
@RequestMapping("/crm/quotedPrice")
@Api(tags = "报价单")
public class CrmQuotedPriceController extends BaseController {

    @Autowired
    private ICrmQuotedPriceService priceService;

    /**
     * 报价单
     * @author 小啊刚
     * @date 2020/6/23
     */
    @GetMapping("page")
    public HttpResult pageQuotedPrice(Page page, CrmQuotedPrice price) {
        Page<CrmQuotedPrice> pricePage = priceService.pageQuotedPrice(page, price);
        return HttpResult.ok(pricePage);
    }

    /**
     * 保存报价单
     * @author 小啊刚
     * @date 2020/6/24
     */
    @GetMapping("save")
    public HttpResult saveQuotedPrice(CrmQuotedPrice price, List<CrmQuotedPriceDetails> priceDetails) {
        priceService.saveQuotedPrice(price, priceDetails);
        return HttpResult.ok();
    }

    /**
     * 停用报价单
     */
    @GetMapping(value = "disable")
    public HttpResult disableSelected(String ids) {
        if (StringUtils.isNoneBlank(ids)) {
            for (String id : ids.split(",")) {
                CrmQuotedPrice crmQuotedPrice = new CrmQuotedPrice();
                crmQuotedPrice.setId(Long.parseLong(id));
                crmQuotedPrice.setStatus(2);
                priceService.updateById(crmQuotedPrice);
            }
        }
        return HttpResult.ok("停用报价单成功");
    }

    /**
     * 启用报价单
     */
    @GetMapping(value = "enable")
    public HttpResult enableSelected(String ids) {
        if (StringUtils.isNoneBlank(ids)) {
            for (String id : ids.split(",")) {
                CrmQuotedPrice crmQuotedPrice = new CrmQuotedPrice();
                crmQuotedPrice.setId(Long.parseLong(id));
                crmQuotedPrice.setStatus(0);
                priceService.updateById(crmQuotedPrice);
            }
        }
        return HttpResult.ok("启用报价单成功");
    }

    /**
     * 删除报价单
     * @author 小啊刚
     * @date 2020/6/24
     */
    @GetMapping("delete")
    public HttpResult delete(String ids) {
        if (StringUtils.isNoneBlank(ids)) {
            for (String id : ids.split(",")) {
                priceService.removeById(id);
            }
        }
        return HttpResult.ok("删除成功");
    }
}
